//
//  CustomerOrderDetailVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/12.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CustomerOrderDetailVc.h"
#import "BdTV.h"
#import "config.h"
#import "UIImageView+WebCache.h"
#import "OrderDM.h"
#import "OrderDetailData.h"
#import "NodataCell.h"
#import "NSString+StringHight.h"
#import "CustomerOrderDetailCell1.h"
#import "OrderDetailCastCell.h"
#import "OrderDetailFootView.h"
#import "NSString+category.h"


@interface CustomerOrderDetailVc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UIImageView *headImgView;
@property (nonatomic,strong)UITextView *mobileLab;
@property (nonatomic,strong)UILabel *nameLab,*dolLab,*dwLab;
@property (nonatomic,strong)OrderDM *dm;
@property (nonatomic,strong)OrderDetailData *mainData;
@property (nonatomic,strong)NodataCell *nodataCell;
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)OrderDetailFootView *footview;


@end

@implementation CustomerOrderDetailVc
@synthesize dm,mainData,nodataCell,tableView,order;



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self refreshData];
}

-(void)initView{
    [super initView];
    

    [[self.titleView doneBtn] removeFromSuperview];
    [self.titleView setMianTitle:@"订单详情"];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, PMW, self.view.frame.size.height-NAV_HIGHT-20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView setSeparatorColor:[UIColor clearColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    if (isIOS7) {
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        }
    }
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView setBackgroundColor:UIColorFromRGB(0xf3f3f3)];
    [self.view addSubview:self.tableView];
    
    
    UIView *faView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-NAV_HIGHT-20-40, PMW, 40)];
    [faView setBackgroundColor:[UIColor whiteColor]];
    
    
//    self.dolLab = [[UILabel alloc]initWithFrame:CGRectMake(10,0 , CGRectGetWidth(faView.frame), faView.frame.size.height)];
//    [self.dolLab setFont:[UIFont systemFontOfSize:16]];
//    [self.dolLab setBackgroundColor:[UIColor clearColor]];
////    [self.dolLab setTextColor:UIColorFromRGB(0x999999)];
//    [self.dolLab setTextColor:[UIColor redColor]];
//    [faView addSubview:self.dolLab];
//    
//    
//    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, faView.frame.size.height - 1, faView.frame.size.width, 1)];
//    [line setBackgroundColor:UIColorFromRGB(0xe3e3e3)];
//    [faView addSubview:line];
    
    
    self.footview = [OrderDetailFootView createView];
    self.footview.frame = CGRectMake(0, 0, PMW, self.footview.frame.size.height);
    
    [self.tableView setTableFooterView:self.footview];
    
    dm = [[OrderDM alloc]init];
    
}



-(void)refreshData{
    
    [nodataCell setState:nodataCellStateLoading];
    __weak typeof(self) bself = self;
    [dm refreshDetailData:self.order.orderCode withCallBack:^(ResulteMod *resultMod) {
        
        if (resultMod.resltState==ResultNetError) {
            
            [bself.nodataCell setState:nodataCellStateNetError];
            
        }else if (resultMod.resltState==ResultFail){
            
            [bself.nodataCell setCodeErrorTip:resultMod.resultCode];
            
        }else{
            
            bself.mainData = resultMod.dataMod;
            if (bself.mainData == nil) {
                [bself.nodataCell setState:nodataCellStateNodata];
            }
            bself.mainData.time = bself.order.time;
            [self upFootView];
        }
        
        [bself.tableView reloadData];
        
    }];
}



-(void)btnOnClick:(UIButton *)sender{
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag==56){
        [self refreshData];
    }
}


#pragma mark tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        return 0;
    }else{
        return 15;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 15)];
    [footView setBackgroundColor:[UIColor clearColor]];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 14, footView.frame.size.width, 1)];
    [line setBackgroundColor:UIColorFromRGB(0xe3e3e3)];
    [footView addSubview:line];
    
    return footView;
}




-(void)upFootView{
    
    if (mainData.state==Progress1) {
        
        [self.footview.phoneText setAttributedText:[NSString makeLineStyle:mainData.buyerMobile color:UIColorFromRGB(0x333333) fontSize:15]];
        self.footview.nameLab.text = mainData.buyerName;
        self.footview.moneyLab.text = [NSString stringWithFormat:@"$%@",mainData.intentionPrice];
        
    }else if(mainData.state==Progress8||mainData.state==Progress9||mainData.state==Progress7){
    
        [self.tableView setTableFooterView:nil];
    }else{
        
        [self.footview.phoneText setAttributedText:[NSString makeLineStyle:mainData.buyerMobile color:UIColorFromRGB(0x333333) fontSize:15]];
        self.footview.nameLab.text = mainData.buyerName;
        self.footview.moneyLab.hidden = YES;
        self.footview.labe1.hidden = YES;
        self.footview.label2.hidden = NO;
        self.footview.label2.text = [NSString stringWithFormat:@"房源锁定"];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (mainData.itemArray&&mainData.itemArray.count>0) {
        
        if (indexPath.section==0&&indexPath.row==0) {
            return [CustomerOrderDetailCell1 getHight:nil withTableView:self.tableView];
        }else{
            return [OrderDetailCastCell getHight:nil withTableView:self.tableView];
        }
    }
    
    return self.tableView.frame.size.height;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (mainData.itemArray&&mainData.itemArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.mainData.itemArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (mainData.itemArray&&mainData.itemArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        
        if (section==0) {
            return [[self.mainData.itemArray objectAtIndex:section] count]+1;
        }else{
            return [[self.mainData.itemArray objectAtIndex:section] count];
        }
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (mainData.itemArray.count==0) {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    
    
    
    if (indexPath.section==0&&indexPath.row==0) {
        CustomerOrderDetailCell1 *cell = [CustomerOrderDetailCell1 cellWithTableView:self.tableView];
        [cell setData:mainData];
        return cell;
        
    }else if(indexPath.section==0){
        
        OrderDetailCastCell *cell = [OrderDetailCastCell cellWithTableView:self.tableView];
        [cell setData:[[mainData.itemArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row-1]];
        return cell;
        
    }else{
        
        OrderDetailCastCell *cell = [OrderDetailCastCell cellWithTableView:self.tableView];
        [cell setData:[[mainData.itemArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
        return cell;
    }
}

@end
